package com.crm.util;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail{

	public static final String host="smtp.gmail.com";
	public static final String port ="465";
	
	public void sendMail( String email,String pass,String m_text, String m_subject,String m_to, File file,String fileName) throws AddressException, MessagingException{
			sendMail(email,pass,host,port,  m_text, m_subject, m_to,file,fileName);
		
	}
	public  void sendMailNoFile(String email,String pass, String m_text, String m_subject,String m_to,String ccEmail) throws AddressException, MessagingException{
			sendMailWithOutFile(email,pass,host,port,  m_text, m_subject, m_to,ccEmail);
	}
	
	
	public static void sendMail(String d_email,String pass, String d_host, String d_port, String m_text, String m_subject,String m_to,File file,String fileName) throws AddressException, MessagingException{
		Properties props = new Properties();
		props.put("mail.smtp.user", d_email);
		props.put("mail.smtp.host", d_host);
		props.put("mail.smtp.port", d_port);
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", d_port);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
			Authenticator auth = new SMTPAuthenticator(d_email,pass);
			Session session = Session.getInstance(props, auth);
			
			//System.out.println("subject is:"+m_subject+"\n Body Is :"+m_text+"\n File Name Is :"+fileName);
			// Define message
		    MimeMessage message =new MimeMessage(session);
		    message.setFrom(new InternetAddress(d_email));
		    //message.setHeader("Content-Type" ,"text/html" );
		    message.addRecipient(Message.RecipientType.TO,new InternetAddress(m_to));
		    message.setSubject(m_subject);

		    // create the message part 
		    MimeBodyPart messageBodyPart = new MimeBodyPart();

		    //fill message
		    messageBodyPart.setContent(m_text,"text/html");

		    Multipart multipart = new MimeMultipart();
		    multipart.addBodyPart(messageBodyPart);

		    if (fileName!=null &&  fileName.trim().length()>0) {
				// Part two is attachment
				messageBodyPart = new MimeBodyPart();
				DataSource source = new FileDataSource(file);
				messageBodyPart.setDataHandler(new DataHandler(source));
				messageBodyPart.setFileName(fileName);
				multipart.addBodyPart(messageBodyPart);
			}
			// Put parts in message
		    message.setContent(multipart);

		    // Send the message
		    
		    Transport.send( message );
		   
			System.out.println("Mail Sent");
		
		}
	public static void sendMailWithOutFile(String d_email,String pass, String d_host, String d_port, String m_text, String m_subject,String m_to,String ccEmail) throws AddressException, MessagingException{
		Properties props = new Properties();
		props.put("mail.smtp.user", d_email);
		System.out.println("User Email Is:"+d_email);
		props.put("mail.smtp.host", d_host);
		System.out.println("User Host Is :"+d_host);
		props.put("mail.smtp.port", d_port);
		System.out.println("User Port Is:"+d_port);
		props.put("mail.smtp.starttls.enable","true");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.socketFactory.port", d_port);
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		
		
		
		
			Authenticator auth = new SMTPAuthenticator(d_email,pass);
			Session session = Session.getInstance(props, auth);


			// Define message
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(d_email));
			message.addRecipient(Message.RecipientType.TO,new InternetAddress(m_to));
			if(ccEmail!=null){
				message.addRecipient(Message.RecipientType.CC,new InternetAddress(ccEmail));
			}
			message.setSubject(m_subject);
			message.setContent(m_text,"text/html");
			System.out.println("From Email Address Is:"+d_email+"\n Receipients Email Address Is :"+m_to+"\n Subject Is:"+m_subject+"\n Text Is:"+m_text);
			// Send message
			Transport.send(message);
			
			System.out.println("Mail Sent");
		
	}
		
	}
	
	/*public static void main(String[] args) {
		//SendMail.sendMail("vishnu2sms@gmail.com", "vishnu123", "smtp.gmail.com", "465", "Hello test mail", "Testing", "sainathreddy@advaithsoft.com");
		SendMail.sendMail("Hello test mail", "Testing1", "sainathreddy@advaithsoft.com");
		System.out.println("Sent mail");
	}*/
	
